
*teacher value added type symmetry graph (Figure 1 panel b)

use "$project\manager\cvrnryear_fe_over5", clear

ren pnr pnrm
merge m:1 pnrm using "$project\managerfe_1step_over5", keep(1 3) keepus(me me_bayes) nogen

tsset cvrnr year

gen d_me=me_bayes - L.me_bayes
gen d_fe=fe-L.fe

reg d_fe d_me if d_me!=0 

binscatter d_fe d_me if d_me!=0, n(50) xtitle("Change in manager effects")  ytitle("Change in wage premia")

reg d_fe d_me if d_me!=0



use "$home\persondata_v2", clear
drop if cvrnr==. 

tsset pnr year
gen samefirm=(cvrnr==L.cvrnr)

ren pnr pnr0
merge m:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) nogen
gen internal=(pnr==pnr0 & samefirm==1)
bysort cvrnr year: egen maxinternal=max(internal)
merge m:1 cvrnr year using "$project\manager\cvrnryear_fe_over5", keep(1 3) keepus(fe) nogen

gen log_income=log(lonind)
drop if log_income==.
drop if pnr==.
reg log i.year
predict wage, r
bysort pnr: egen manager_meanwage=mean(wage)
bysort pnr: egen nyear=nvals(year)
egen spell=group(pnr0 cvrnr)
ren pnr pnrm
merge m:1 pnrm using "$project\managerfe_1step_over5", keepus(me_bayes) keep(1 3) nogen
xtile q_meanwage=me_bayes, n(4)
tsset spell year
ren pnrm pnr

*Figure 1 panel a

sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==1&pnr==F.pnr&pnr!=L.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==2&pnr==F.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==3&pnr==F.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==4&pnr==F.pnr

sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==1&pnr==F.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==2&pnr==F.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==3&pnr==F.pnr
sum L2.fe L.fe fe F.fe if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==4&pnr==F.pnr&pnr!=L.pnr

sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==1&pnr==F.pnr&pnr!=L.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==2&pnr==F.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==3&pnr==F.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==1&q_meanwage==4&pnr==F.pnr

sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==1&pnr==F.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==2&pnr==F.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==3&pnr==F.pnr
sum L2.wage L.wage wage F.wage if L2.pnr==L.pnr&L.q_meanwage==4&q_meanwage==4&pnr==F.pnr&pnr!=L.pnr



*retirement and death 

use "$project\merger_cvrnr", clear
cap drop pe
merge m:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) nogen
ren pnr pnrm 
merge m:1 pnrm using "$project\managerfe_1step_over5", keepus(me_bayes) keep(1 3) nogen

merge m:1 cvrnr year using "$project\fire", keep(1 3) keepus(arf at) nogen
tsset cvrnr year
gen roa=arf/L.at

merge m:1 cvrnr year using "$project/wagedistribution", keep(1 3) nogen

foreach var in roa p10 p50 p90 {
	sum `var',d
	replace `var'=r(p1) if `var'<r(p1)
	replace `var'=r(p99) if `var'>r(p99) & `var'!=.
}


drop if pnrm==.
gen death_year=year+1
ren pnrm pnr
merge 1:1 pnr death_year using "I:\Workdata\702525\Alex\DLM\deaths.dta", keep(1 3) nogen

merge 1:1 pnr year using "$home\persondata_v2", keep(1 3) keepus(aldernov ansaar) nogen
ren alder age

xtile quantile_manager=me_bayes, n(4)

tsset cvrnr year

gen retire_lowfe = (L2.quantile==1&L.quantile==1&pnr!=L.pnr&L.age>=62& (L.death_age==.& L2.death_age==.))
gen death_lowfe = (L2.quantile==1&L.quantile==1&pnr!=L.pnr& (L.death_age!=.|L2.death_age!=.))
gen retire_highfe = (L2.quantile==4&L.quantile==4&pnr!=L.pnr&L.age>=62& (L.death_age==.& L2.death_age==.))
gen death_highfe = (L2.quantile==4&L.quantile==4&pnr!=L.pnr& (L.death_age!=.|L2.death_age!=.))

merge m:1 cvrnr year using "$project\firmdata_v1", keep(1 3) keepus(vapp) nogen

bysort cvrnr: egen nevent_retire_lowfe=sum(retire_lowfe)
bysort cvrnr: egen nevent_retire_highfe=sum(retire_highfe)
bysort cvrnr: egen nevent_death_lowfe=sum(death_lowfe)
bysort cvrnr: egen nevent_death_highfe=sum(death_highfe)

compress

foreach var in retire_lowfe retire_highfe {

preserve

gen event=`var'
bysort cvrnr: egen year_event=sum(event*year)
keep if nevent_`var'<=1
keep if nevent_retire_highfe+nevent_retire_lowfe+nevent_death_highfe+nevent_death_lowfe==nevent_`var'

gen tau=year-year_event
replace tau=-1 if year_event==0

gen taub4= (tau<=-4)
gen taub3= (tau==-3)
gen taub2 = (tau==-2)
forv i=0/5 {
    gen byte tau`i'=(tau==`i')
}
gen tau6=(tau>=6)

egen ind36num=group(ind36)

eststo clear

foreach var in income vapp roa p10 p50 p90 {
eststo: reghdfe `var' taub4-tau6 if tau^2<50 [aw=nworker], absorb(cvrnr year#ind36num)
}

estout using "$project\results\event_retiredeath.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) append

restore

}



foreach var in death_lowfe death_highfe {

preserve

gen event=`var'
bysort cvrnr: egen year_event=sum(event*year)
keep if nevent_`var'<=1
keep if nevent_retire_highfe+nevent_retire_lowfe+nevent_death_highfe+nevent_death_lowfe==nevent_`var'

gen tau=year-year_event
replace tau=-1 if year_event==0

gen taub4= (tau<=-4)
gen taub3= (tau==-3)
gen taub2 = (tau==-2)
forv i=0/5 {
    gen byte tau`i'=(tau==`i')
}
gen tau6=(tau>=6)

egen ind36num=group(ind36)

eststo clear

foreach var in income {
eststo: reghdfe `var' taub4-tau6 if tau^2<50 [aw=nworker], absorb(cvrnr year#ind36num)
}

estout using "$project\results\event_retiredeath.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) append

restore

}

